package org.aeon.gamechatnest.handler;

import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.aeon.gamechatnest.common.response.RespStatus;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.exception.ApplicationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import static org.aeon.gamechatnest.common.response.RespStatus.*;


/**
 * @author: MissingAeon
 * @date: 2024/5/8 18:29
 * @description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 访问被拒绝
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleRRException(AccessDeniedException e) {
        return handleProgram(NO_PERMISSION);
    }

    /**
     * 身份验证异常
     */
    @ExceptionHandler(AuthenticationException.class)
    public Result handleRRException(AuthenticationException e) {
        return handleProgram(e);
    }

    /**
     * 绑定异常
     */
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        return handleProgram(BIND_ERROR, e.getMessage());
        // return null;
    }


    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return handleProgram(PARAM_ERROR, e.getMessage());

    }

    /**
     * 验证异常
     */
    @ExceptionHandler(ValidationException.class)
    public Result handleValidationException(ValidationException e) {
        return handleProgram(VALIDATION_ERROR, e.getMessage());

    }

    /**
     * 约束违反异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result handleConstraintViolationException(ConstraintViolationException e) {
        return handleProgram(CONSTRAINT_ERROR, e.getMessage());
    }


    /**
     * 缺少请求体
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return handleProgram(CONSTRAINT_ERROR, "缺少请求体");
    }

    /**
     * 重复密钥异常
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        return handleProgram(DUPLICATE_KEY_ERROR);
    }

    /**
     * 不支持Http请求方法异常
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handlerNoFoundException(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return handleProgram(REQUEST_METHOD_ERROR);
    }

    /**
     * 未找到处理程序异常
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result handlerNoFoundException(NoHandlerFoundException e) {
        log.error(e.getMessage(), e);
        return handleProgram(NO_HANDLER_FOUND_ERROR);
    }

    /**
     * 总异常
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        if (e instanceof ApplicationException) {
            return handleProgram((ApplicationException) e);
        }
        return handleProgram(ERROR);

    }

    private Result handleProgram(ApplicationException e) {
        return handleProgram(RespStatus.find(e.getCode()), e.getMessage());
    }

    private Result handleProgram(AuthenticationException e) {
        return handleProgram(AUTHENTICATION_ERROR, e.getMessage());
    }

    private Result handleProgram(RespStatus state) {
        return handleProgram(state, null);
    }

    private Result handleProgram(RespStatus state, String messages) {
        return ResultBuild.result()
                .throwAnError()
                .state(state)
                .message(messages)
                .timestamp()
                .build();
    }
}
